from UserModel import UserModel
from util import session,get_dict_list_from_sqlalchemy_result
from sqlalchemy import distinct
from sys import maxsize

class UserManageDao:
    @staticmethod
    def add_user(**kwargs):
        return UserModel.create(**kwargs)

    @staticmethod
    def del_user(uuid):
        user = UserModel.get(uuid = uuid).first()
        user.delete()

    @staticmethod
    def get_user(uuid):
        return UserModel.get(uuid = uuid).first()

    @staticmethod
    def get_all_user():
        return UserModel.get().all()

    @staticmethod
    def search_user(**kwargs):
        return UserModel.get(**kwargs).all()
   

    @staticmethod
    def search_user_page(withCount: bool = False, pageIndex: int = 1, pageSize: int = maxsize, **kwargs):
        count = [0, UserModel.get(**kwargs).count()][withCount]
        result = UserModel.get_page(pageIndex, pageSize, **kwargs).all()
        return count, result

    @staticmethod
    def get_groups():
        return get_dict_list_from_sqlalchemy_result(session.query(UserModel.group).distinct(UserModel.group).all())